package com.achievement.controller;

import com.achievement.domain.dto.MeetingDTO;
import com.achievement.domain.param.MeetingParam;
import com.achievement.domain.vo.MeetingVO;
import com.achievement.service.IMeetingService;
import com.alibaba.fastjson2.JSONObject;
import com.common.annotation.Anonymous;
import com.common.annotation.Log;
import com.common.core.controller.BaseController;
import com.common.core.domain.AjaxResult;
import com.common.core.page.TableDataInfo;
import com.common.enums.BusinessType;
import com.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "学术交流-主办会议")
@RestController
@RequestMapping("/achievement/meeting")
public class MeetingController extends BaseController {

    @Autowired
    private IMeetingService meetingService;

    @PreAuthorize("@ss.hasPermi('achievement:meeting:list')")
    @ApiOperation("获取会议列表")
    @GetMapping("/list")
    public TableDataInfo list(MeetingParam param)
    {
        startPage();
        List<MeetingVO> list = meetingService.selectByPage(param);
        return getDataTable(list);
    }

    /**
     * 左上方统计 通过，退回，待审核
     * @param param 检索参数
     * @return AjaxResult
     */
    @PreAuthorize("@ss.hasPermi('achievement:meeting:list')")
    @ApiOperation(value = "左上方统计 通过，退回，待审核",notes = "左上方统计 通过，退回，待审核")
    @GetMapping(value = "/list-statistics")
    public AjaxResult listStatistics(MeetingParam param)
    {
        List<HashMap<String, Integer>> list =meetingService.listStatistics(param);

        return success(list);
    }

    @PostMapping("/export")
    @PreAuthorize("@ss.hasPermi('achievement:meeting:export')")
    @ApiOperation(value = "主办会议导出", notes = "主办会议导出")
    public void export(HttpServletResponse response, @RequestBody Map<String,Object> requestMap) {
        MeetingParam param = JSONObject.parseObject(JSONObject.toJSONString(requestMap), MeetingParam.class);
        List<String> selectedColumns = (List<String>) requestMap.get("selectedColumns");

        List<MeetingVO> list = meetingService.getListByConditions(param);
        ExcelUtil<MeetingVO> util = new ExcelUtil<>(MeetingVO.class);
        util.showColumn(selectedColumns.toArray(new String[0]));
        util.exportExcel(response, list, "主办会议导出数据");
    }

    /**
     * 获取会议详细信息
     */
    @PreAuthorize("@ss.hasPermi('achievement:meeting:query')")
    @ApiOperation("获取会议详情")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(meetingService.getInfoById(id));
    }

    /**
     * 新增会议
     */
    @PreAuthorize("@ss.hasPermi('achievement:meeting:add')")
    @ApiOperation("新增会议")
    @Log(title = "新增会议", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public AjaxResult add(@RequestBody MeetingDTO dto)
    {
        return meetingService.add(dto);
    }

    /**
     * 修改会议
     */
    @PreAuthorize("@ss.hasPermi('achievement:meeting:edit')")
    @ApiOperation("修改会议")
    @Log(title = "修改会议", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/update")
    public AjaxResult edit(@RequestBody MeetingDTO dto)
    {
        return meetingService.update(dto);
    }

    /**
     * 删除会议
     */
    @PreAuthorize("@ss.hasPermi('achievement:meeting:remove')")
    @ApiOperation("删除会议")
    @Log(title = "删除会议", businessType = BusinessType.DELETE)
    @DeleteMapping("/del/{ids}")
    public AjaxResult remove(@PathVariable List<String> ids)
    {
        return toAjax(meetingService.batchRemove(ids));
    }
}
